﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI.Component
{
    public class PillarGroup : MonoBehaviour
    {
        public GridLayoutGroup Layout;
        public GameObject insObj;
        private List<PillarGroupObject> objList;

        private void Awake()
        {
            if (objList != null)
            {
                return;
            }
            objList = new List<PillarGroupObject>();
            if (Layout == null)
            {
                gameObject.GetComponent<GridLayoutGroup>();
            }
            var childCount = transform.childCount;
            for (int i = 0; i < childCount; i++)
            {
                var child = transform.GetChild(i);
                var comp = child.GetComponent<PillarGroupObject>();
                if (comp == null)
                {
                    comp = child.gameObject.AddComponent<PillarGroupObject>();
                }
                objList.Add(comp);
            }
        }

        public void Refresh(int index, object data)
        {
            if (objList.Count <= index)
            {
                return;
            }

            objList[index].Refresh(data);
        }
        
        public void Refresh(IList list)
        {
            if (objList == null)
            {
                Awake();
            }
             
            for (var i = 1; i < objList.Count -1; i++)
            {
                if (list.Count - i >= 0)
                {
                    objList[objList.Count - i].Refresh(list[list.Count - i]);
                }
            }
        }
    }
}